﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using System.Data;
using System.Data.SqlClient;
using Common;

namespace DAL
{
    public class UrlRouteDAL : DB
    {
        #region 公共属性

        #endregion

        #region 构造函数
        public UrlRouteDAL(LoginInfo info)
        {
            //base.loginInfo = info;
            base.loginInfo = new LoginInfo();
        }

        #endregion

        #region 公共函数

        public UrlRoute GetInfoById(string id)
        {
            HIDB.dbo cmd = base.conn();
            try
            {
                string strSql = @"SELECT [Id],[DBName],[EntityId],[EntityName]
,[Slug],[Sign],[UserID],[Status]
FROM [dbo].[UrlRoute]
WHERE Id=@Id";
                IDataParameter[] cmdParms = { new SqlParameter("@Id", id) };
                DataTable dts = cmd.GetDataTable(strSql, cmdParms);
                var list = dts.MapToList<UrlRoute>();
                if (list != null && list.Count > 0)
                {
                    var info = list[0];
                    return info;
                }
            }
            catch (Exception ex)
            {
                Logger.LogError("[dbo].[UrlRoute]", "GetInfoById", 0, 1, ex, "", "");
            }
            return null;
        }


        public List<UrlRoute> GetList(out int recordCount)
        {
            recordCount = 0;
            List<UrlRoute> result = new List<UrlRoute>();
            try
            {
                HIDB.dbo cmd = base.conn();
                List<SqlParameter> cmdParms = new List<SqlParameter>();
                string strSql = @"SELECT [Id],[DBName],[EntityId],[EntityName]
,[Slug],[Sign],[UserID],[Status] FROM [dbo].[UrlRoute] WHERE 1=1";
                //if (!string.IsNullOrEmpty(funName))
                //{
                //    strSql += " AND FunName like '%'+@FunName+'%'";
                //    cmdParms.Add(new SqlParameter("@FunName", funName));
                //}
                //if (funType > 0)
                //{
                //    strSql += " AND FunType=@FunType";
                //    cmdParms.Add(new SqlParameter("@FunType", funType));
                //} 
                DataTable dt = cmd.GetDataTable(strSql, cmdParms.ToArray());
                result = dt.MapToList<UrlRoute>();
                recordCount = result.Count;
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int Insert(UrlRoute urlRoute)
        {
            int num = -1;
            //创建cmd对象
            HIDB.dbo cmd = base.conn();
            //执行命令返回结果
            try
            {
                string strSql = @"INSERT INTO [dbo].[UrlRoute] ([Id],[DBName],[EntityId],[EntityName],[Slug],[Sign],[UserID],[Status],[CreateTime]) 
VALUES (@Id,@DBName,@EntityId,@EntityName,@Slug,@Sign,@UserID,@Status,getdate())";
                SqlParameter[] cmdParms = new SqlParameter[]
                {
                    new SqlParameter("@Id",urlRoute.Id)
                     ,new SqlParameter("@DBName",urlRoute.DBName)
                     ,new SqlParameter("@EntityId",urlRoute.EntityId)
                     ,new SqlParameter("@EntityName",urlRoute.EntityName??string.Empty)
                     ,new SqlParameter("@Slug",urlRoute.Slug??string.Empty)
                     ,new SqlParameter("@Sign",urlRoute.Sign??string.Empty)
                     ,new SqlParameter("@UserID",urlRoute.UserID??string.Empty)
                     ,new SqlParameter("@Status",urlRoute.Status)
                };
                num = cmd.ExecuteSql(strSql, cmdParms);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return num;
        }

        public int Update(UrlRoute urlRoute)
        {
            int num = -1;

            //创建cmd对象
            HIDB.dbo cmd = base.conn();

            //执行命令返回结果
            try
            {
                string strSql = @"UPDATE [dbo].[UrlRoute]
   SET [DBName] = @DBName
      ,[EntityId] = @EntityId
      ,[EntityName] = @EntityName
      ,[Slug] = @Slug
      ,[Sign] = @Sign
      ,[UserID] = @UserID
      ,[Status] = @Status
 WHERE [Id] = @Id";
                SqlParameter[] cmdParms = new SqlParameter[]
                {
                    new SqlParameter("@Id",urlRoute.Id)
                     ,new SqlParameter("@DBName",urlRoute.DBName)
                     ,new SqlParameter("@EntityId",urlRoute.EntityId)
                     ,new SqlParameter("@EntityName",urlRoute.EntityName??string.Empty)
                     ,new SqlParameter("@Slug",urlRoute.Slug??string.Empty)
                     ,new SqlParameter("@Sign",urlRoute.Sign??string.Empty)
                     ,new SqlParameter("@UserID",urlRoute.UserID??string.Empty)
                     ,new SqlParameter("@Status",urlRoute.Status)
                    
                };
                num = cmd.ExecuteSql(strSql, cmdParms);
            }
            catch (Exception ex)
            {
                Logger.LogError("[dbo].[UrlRoute]", "Update", 0, 1, ex, "", "");
            }
            return num;
        }

        public int Delete(string id)
        {
            int num = -1;
            HIDB.dbo cmd = base.conn();
            try
            {
                string strSql = @"DELETE FROM [dbo].[UrlRoute]
 WHERE [Id]=@Id";
                SqlParameter[] cmdParms = new SqlParameter[]
                {
                     new SqlParameter("@Id",id)
                };
                num = cmd.ExecuteSql(strSql, cmdParms);
            }
            catch (Exception ex)
            {
                Logger.LogError("[dbo].[UrlRoute]", "Delete", 0, 1, ex, "", "");
            }
            return num;
        }

        #endregion
    }
}
